package com.hrt.datasender.process.strategy.hj212;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.btcode.db.result.Row;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BdSendRecordService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.datasender.process.indentity.hj212.MessageModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.hj212.util.DataType;

public class Com9014Process implements IProcessStrategy {

    @Override
    public String getCommandId() {
        // TODO Auto-generated method stub
        return "9014";
    }

    @Override
    public String getStrategyName() {
        return "中心站数据接收反馈";
    }

    @Override
    public Object packet(List<Row> data, BcSendPlatformModel platform) {
        return null;
    }

    @Override
    public Object packetReissue(List<Row> data, BcSendPlatformModel platform) {
        return null;
    }

    @Override
    public void decode(MessageModel data, BcSendPlatformModel sendPlatform) throws Exception {

        String msg = data.getMsg() + "";
        HashMap<String, String> cpValueMap = MessagePraser.getCpValue(msg);

        String qn = cpValueMap.get("QN");
        String cn = cpValueMap.get("CN");
        String ip = sendPlatform.getIp();
        String port = sendPlatform.getPort();
        StorageDataType dataType = null;

        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date dateTime = sf.parse(qn);
        /**
         * 判断返回的QN时间超过当前时间往后推30天的话，认为是补传数据，不进行相应数据更新
         */
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,30);
        Date compareTime = cal.getTime();
        if(dateTime.compareTo(compareTime)>0){
            return;
        }

        if (DataType.second.getCn().equals(cn)) {
            dataType = StorageDataType.SECOND;
        }
        else if (DataType.minute.getCn().equals(cn)) {
            dataType = StorageDataType.MINUTES;
        }
        else if (DataType.hour.getCn().equals(cn)) {
            dataType = StorageDataType.HOUR;
        }
        else if (DataType.day.getCn().equals(cn)) {
            dataType = StorageDataType.DAY;
        }
        else if (DataType.deviceOper.getCn().equals(cn)) {
            dataType = StorageDataType.DEVICE;
        }

        BdSendRecordService.getInstance().updateLastSendDate(dataType, ip, port, dateTime);

    }

}
